<template>
  <div class="">
    <!-- 搜索框 -->
    <el-form :inline="true">

      <el-form-item label="账户余额:">
        <span style="color: red;font-weight: 600;">{{purse}}</span>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-refresh-left" size="small"
          @click="handleSearch" style="background: #FF2832;">
          刷新
        </el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="small"
          @click="recharge" style="background: #FF2832;">充值
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%" v-loading="loading">

      <el-table-column label="创建时间" prop="createTime" align="center" width="200">
      </el-table-column>

      <el-table-column prop="num" label="金额" width="200" align="center">
        <template slot-scope="scope">
          <span style="color: red;font-weight: 600;">{{scope.row.num}}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column prop="context" label="描述" align="left">
      </el-table-column> -->

      <!--    <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">

        </template>
      </el-table-column> -->

    </el-table>

    <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next" :total="total"
      :current-page="page" :page-size="rows" style="margin: 15px;">
    </el-pagination>

  </div>
</template>

<script>
/**
   * author        cyi
   * description
   */
import { addPurse, getRecord } from '@/api/purse'
import { getOneUser } from '@/api/user'
export default {
  name: 'recharge',
  data () {
    return {
      loading: false,
      tableData: [],
      page: 1,
      rows: 5,
      total: 0,
      search: {},
      purse: 0,
      sumBlance: '0.00'
    }
  },
  methods: {
    // 获取充值记录
    getCurRecord () {
      const userId = JSON.parse(localStorage.getItem('userMsg')).id
      getRecord(userId).then(res => {
        console.log(res)
        this.tableData = res.data.data
      })
    },
    getPurse () {
      const id = JSON.parse(localStorage.getItem('userMsg')).id
      getOneUser(id).then(res => {
        this.purse = res.data.data.purse
        console.log(this.purse)
      })
    },
    handleSearch () {
      this.page = 1
      this.getCurRecord()
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTable()
    },
    recharge () {
      const obj = JSON.parse(localStorage.getItem('userMsg'))
      const zid = obj.id
      this.$prompt('请输入充值金额', '充值', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        // value 为充值金额
        const params = {
          id: zid,
          num: value
        }
        addPurse(params).then(res => {
          if (res.data.meta.status === 200) {
            this.$message({
              message: '充值成功',
              type: 'success'
            })
            this.getPurse()
          } else {
            this.$message({
              message: '充值失败',
              type: 'warning'
            })
          }
        })
      })
    }
  },
  created () {
    // this.getBalance()
    // this.getTable()
    this.getPurse()
    this.getCurRecord()
  }
}
</script>

<style>

</style>
